package com.lzp.controller;

import com.lzp.pojo.vo.CommentLevelCountsVO;
import com.lzp.pojo.vo.ItemInfoVO;
import com.lzp.pojo.vo.ShopcartVO;
import com.lzp.service.ItemService;
import com.lzp.utils.PagedGridResult;
import com.lzp.utils.ServerResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品相关接口
 *
 * @author dtyy
 * @date 2020/8/4
 */
@Api(value = "商品接口", tags = "商品相关接口")
@RestController
@RequestMapping("items")
public class ItemController extends BaseController {

    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public ServerResponse info(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @PathVariable String itemId) {

        if (StringUtils.isBlank(itemId)) {
            return ServerResponse.createByErrorMsg("商品id为空");
        }

        // 封装商品详情VO
        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(itemService.getItemById(itemId));
        itemInfoVO.setItemImgList(itemService.listItemImg(itemId));
        itemInfoVO.setItemSpecList(itemService.listItemSpec(itemId));
        itemInfoVO.setItemParams(itemService.getItemParam(itemId));

        return ServerResponse.createBySuccess(itemInfoVO);
    }

    @ApiOperation(value = "查询商品评价数", notes = "查询商品评价数", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public ServerResponse commentLevel(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId) {

        if (StringUtils.isBlank(itemId)) {
            return ServerResponse.createByErrorMsg("商品id为空");
        }

        CommentLevelCountsVO countsVO = itemService.getCommentCounts(itemId);
        return ServerResponse.createBySuccess(countsVO);
    }

    @ApiOperation(value = "查询商品评论", notes = "查询商品评论", httpMethod = "GET")
    @GetMapping("/comments")
    public ServerResponse comments(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId,
            @ApiParam(name = "level", value = "评价级别", required = false)
            @RequestParam Integer level,
            @ApiParam(name = "page", value = "第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "每页条数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(itemId)) {
            return ServerResponse.createByErrorMsg("商品id为空");
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult result = itemService.queryPagedComments(itemId, level, page, pageSize);
        return ServerResponse.createBySuccess(result);
    }

    @ApiOperation(value = "搜索商品", notes = "搜索商品", httpMethod = "GET")
    @GetMapping("/search")
    public ServerResponse search(
            @ApiParam(name = "keywords", value = "搜索关键词", required = true)
            @RequestParam String keywords,
            @ApiParam(name = "sort", value = "排序规则", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "每页条数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank("keywords")) {
            return ServerResponse.createByErrorMsg("关键字不能为空");
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = SEARCH_PAGE_SIZE;
        }

        PagedGridResult result = itemService.searchItems(keywords, sort, page, pageSize);
        return ServerResponse.createBySuccess(result);
    }

    @ApiOperation(value = "通过分类id搜索商品列表", notes = "通过分类id搜索商品列表", httpMethod = "GET")
    @GetMapping("/catItems")
    public ServerResponse catItems(
            @ApiParam(name = "catId", value = "三级分类id", required = true)
            @RequestParam Integer catId,
            @ApiParam(name = "sort", value = "排序规则", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "每页条数", required = false)
            @RequestParam Integer pageSize) {

        if (catId == null) {
            return ServerResponse.createByErrorMsg("分类id不能为空");
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = SEARCH_PAGE_SIZE;
        }

        PagedGridResult result = itemService.searchItems(catId, sort, page, pageSize);
        return ServerResponse.createBySuccess(result);
    }

    @ApiOperation(value = "根据商品规格id查询商品数据", notes = "根据商品规格id查询商品数据", httpMethod = "GET")
    @GetMapping("/refresh")
    public ServerResponse refresh(
            @ApiParam(name = "itemSpecIds", value = "商品规格id", required = false)
            @RequestParam String itemSpecIds) {

        if (StringUtils.isBlank(itemSpecIds)) {
            ServerResponse.createBySuccessMsg("购物车为空");
        }

        List<ShopcartVO> list = itemService.queryItemsBySpecIds(itemSpecIds);
        return ServerResponse.createBySuccess(list);
    }

}
